﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class FormMin : Form
    {
        public FormMin()
        {
            InitializeComponent();
        }

        private void FormMin_Load(object sender, EventArgs e)
        {
            var sql = "select * from Articles";
            var dt = DbHelper.GetDataTable (sql);
            dataGridView1.DataSource = dt;
            dataGridView1.ReadOnly = true;
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dataGridView1.AllowUserToAddRows = false;

        }

        private void button1_Click(object sender, EventArgs e)//查
        {
            var name = textBox1.Text;
            var sever = string.Format("select * from Articles where Author like '%{0}%'", name);
            var dt = DbHelper.GetDataTable (sever);
            dataGridView1.DataSource = dt;

        }

        private void button4_Click(object sender, EventArgs e)//删除
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                var nex = dataGridView1.SelectedRows[0];
                var id = (int)nex .Cells["Id"].Value;
                var Deletenex = MessageBox.Show("确定要删除当前选中的行吗？"
                    , "提示", MessageBoxButtons.YesNo
                    , MessageBoxIcon.Information);
                if (Deletenex.Equals(DialogResult.Yes))
                {
                    var sql = string.Format("delete from Articles where Id={0}", id);
                    var was = DbHelper.AddOrEditSave(sql);
                    if (was > 0)
                    {
                        var name = textBox1.Text;
                        var sqls = string.Format("select * from Articles where Author like '%{0}%'", name);
                        var dt = DbHelper.GetDataTable(sqls);
                        dataGridView1.DataSource = dt;
                        MessageBox.Show("删除成功!");
                    }
                }
            }
            else
            {
                MessageBox.Show("您没选择行，请选择一条行！");
            }

        }

        private void button2_Click(object sender, EventArgs e)//添加
        {
           Form3  have = new Form3();
            var res = have .ShowDialog();
            if (res == DialogResult.Yes)
            {
                var name = textBox1.Text;
                var sqlstu = string.Format("select * from Articles where Author like '%{0}%'", name);
                var dt = DbHelper.GetDataTable(sqlstu);
                dataGridView1.DataSource = dt;
            }
            else
            {
                MessageBox.Show("您已取消添加！");
            }
        }

        private void button3_Click(object sender, EventArgs e)//更新
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                var id = (int)dataGridView1.SelectedRows[0].Cells["Id"].Value;
                var Title = (string)dataGridView1.SelectedRows[0].Cells["Title"].Value;
                var Author = (string)dataGridView1.SelectedRows[0].Cells["Author"].Value;
                var CreatedTime = dataGridView1.SelectedRows[0].Cells["CreatedTime"].Value;
                var asctime = CreatedTime.ToString();
                Form3 form3 = new Form3(id ,Title ,Author , asctime);
                var read = form3 .ShowDialog();
                if (read == DialogResult.Yes)
                {
                    var name = textBox1.Text;
                    var str = string.Format("select * from Articles where Author like '%{0}%'", name);
                    var dt = DbHelper.GetDataTable(str);
                    dataGridView1.DataSource = dt;
                }
                else
                {
                    MessageBox.Show("失败！");
                }
            }
            else
            {
                MessageBox.Show("请选择行！");
            }


        }
    }
}
